package com.woniuxy.joylifeforum.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.CenterUserExample;
import com.woniuxy.common.entity.ForumForum;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.ForumForumMapper;
import com.woniuxy.joylifeforum.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@Service
public class UserCenterServiceIml implements UserCenterService {
    @Autowired
    private CenterUserMapper centerUserMapper;
    @Autowired
    private ForumForumMapper forumForumMapper;
    /*
    获取用户基本信息
     */
    @Override
    public Map<String, Object> findUserMes(String userId) {
        //用户信息
        CenterUser centerUser = centerUserMapper.selectByPrimaryKey(userId);
        //社区信息
        ForumForum forumForum=null;
        if(centerUser.getForumId()!=null){
            forumForum = forumForumMapper.selectByPrimaryKey(centerUser.getForumId());
        }
        Map<String,Object> map =new HashMap<>();
        map.put("centerUser",centerUser);
        map.put("forumForum",forumForum);
        return map;
    }
    /*
    用户信息修改
     */
    @Override
    public Map<String, Object> userMesSubmitService(String userId, CenterUser user) {
        //信息内容判断
        String regex = "(\\w{3,10}\\.)*\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
        Pattern pattern = Pattern.compile("[0-9]*");
        //验证 昵称 手机 邮箱
        Map<String,Object>map=new HashMap<>();
        if(user.getNickname().length()>8||user.getNickname().length()==0){
            map.put("status",0);
            map.put("mes","昵称1-8位");
            return map;
        }else if(user.getPhone().length()!=11||!pattern.matcher(user.getPhone()).matches()){
            map.put("status",0);
            map.put("mes","电话应为11位数字");
            return map;
        }else if(!user.getEmail().matches(regex)){
            map.put("status",0);
            map.put("mes","邮箱格式不正确");
            return map;
        }
        //验证手机号
        CenterUserExample userExample=new CenterUserExample();
        CenterUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdNotEqualTo(userId);
        criteria.andPhoneEqualTo(user.getPhone());
        long l = centerUserMapper.countByExample(userExample);
        if(l!=0){
            map.put("status",0);
            map.put("mes","该手机号已注册");
            return map;
        }
        //信息修改
        CenterUser centerUser = centerUserMapper.selectByPrimaryKey(userId);
        centerUser.setPhoto(user.getPhoto());
        centerUser.setNickname(user.getNickname());
        centerUser.setEmail(user.getEmail());
        centerUser.setPhone(user.getPhone());
        centerUser.setIntro(user.getIntro());
        centerUserMapper.updateByPrimaryKey(centerUser);
        map.put("status",1);
        map.put("mes","已修改");
        return map;
    }
}
